﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WindowsFormApplication.FormBase;
using WindowsFormsView;
using WindowsFormsPresenter.AccountingObjectCategory;
using WindowsFormsModel.BusinessObjects;
using Infragistics.Win.UltraWinTree;
using Utils;
using Infragistics.Win.UltraWinEditors;

namespace WindowsFormApplication.FormCategoryTree
{
    public partial class frmVendorCategoryList : frmBaseTreeList, IAccountingObjectCategoriesView
    {
        private AccountingObjectCategoriesPresenter _accountingObjectCategoriesPresenter;
        private bool IsCustomer = false;
        public IList<AccountingObjectCategoryModel> AccountingObjectCategories
        {
            set
            {
                DataSet ds = new DataSet();
                ds = value.AsDataSet(TableName);

                ds.Relations.Add(RelationName, ds.Tables[TableName].Columns[TablePrimaryKey], ds.Tables[TableName].Columns[TableParentKey], false);
                treeMain.DataSource = ds.Tables[TableName];

                UltraTreeNode node = new UltraTreeNode();
                node = treeMain.TopNode;

                if (treeMain.Nodes.Count > 0)
                {
                    while (node != null)
                    {
                        node.DataKey = node.Cells[TablePrimaryKey].Value;
                        node.Key = node.DataKey.ToString();
                        node = node.NextVisibleNode;
                    }
                    treeMain.Nodes[0].Selected = true;
                }

                treeMain.ColumnSettings.ColumnSets[0].Columns["AccountingObjectCategoryID"].Visible = false;
                treeMain.ColumnSettings.ColumnSets[0].Columns["ParentID"].Visible = false;
                treeMain.ColumnSettings.ColumnSets[0].Columns["IsParent"].Visible = false;
                treeMain.ColumnSettings.ColumnSets[0].Columns["IsCustomer"].Visible = false;
                treeMain.ColumnSettings.ColumnSets[0].Columns["RestaurantID"].Visible = false;

                treeMain.ColumnSettings.ColumnSets[0].Columns["AccountingObjectCategoryCode"].Text = "Mã loại NCC";
                treeMain.ColumnSettings.ColumnSets[0].Columns["AccountingObjectCategoryName"].Text = "Tên loại NCC";
                treeMain.ColumnSettings.ColumnSets[0].Columns["Description"].Text = "Mô tả";
                treeMain.ColumnSettings.ColumnSets[0].Columns["IsActive"].Text = "Được sử dụng";

                UltraCheckEditor chkIsActive = new UltraCheckEditor();
                chkIsActive.CheckAlign = ContentAlignment.MiddleCenter;
                treeMain.ColumnSettings.ColumnSets[0].Columns["IsActive"].EditorComponent = chkIsActive;
            }
        }

        protected override void InitData()
        {
            try { _accountingObjectCategoriesPresenter.Display(IsCustomer); }
            catch (ApplicationException ex) { ShowMessageBox(ex.Message, "Thông báo lỗi từ hệ thống !", MessageBoxButtons.OK, MessageBoxIcon.Error); }
        }

        public frmVendorCategoryList()
        {
            InitializeComponent();

            _accountingObjectCategoriesPresenter = new AccountingObjectCategoriesPresenter(this);
        }

        protected override void DeleteRow()
        {
            try
            {
                if (treeMain.ActiveNode != null)
                {
                    string accountingObjectCategoryId = treeMain.ActiveNode.Cells[TablePrimaryKey].Value.ToString();
                    new AccountingObjectCategoryPresenter(null).Delete(accountingObjectCategoryId);
                    ShowMessageBox("Xóa dữ liệu hoàn thành !", "Thông báo xóa dữ liệu !", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    ShowMessageBox("Bạn chưa chọn dòng để xóa !", "Thông báo xóa dữ liệu !", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
            }
            catch (ApplicationException ex) { ShowMessageBox(ex.Message, "Thông báo lỗi hệ thống !", MessageBoxButtons.OK, MessageBoxIcon.Error); }
        }
    }
}
